//
//  ItemDataCenter.swift
//  Persistance
//
//  Created by apple on 2018/10/27.
//  Copyright © 2018 apple. All rights reserved.
//

import UIKit

class ItemDataCenter: NSObject {

    private var itemTable = ItemTable()
    private var itemDetailTable = ItemDetailTable()
    
    public func createNewItem() -> ItemView {
        
        let itemRecord = ItemRecord()
        itemRecord.name = "tree"
        try! self.itemTable.insertRecord(itemRecord)
        
        let itemDetailRecord = ItemDetailRecord()
        itemDetailRecord.detail = "this is a very tall tree"
        try! self.itemDetailTable.insertRecord(itemDetailRecord)
        
        let itemView = ItemView()
        itemView.itemRecordPrimaryKey = itemRecord.primaryKey
        itemView.itemDetailRecordPrimaryKey = itemDetailRecord.primaryKey
        
        itemView.mergeRecord(itemRecord, shouldOverride: true).mergeRecord(itemDetailRecord, shouldOverride: true)
        
        return itemView
    }
    
    public func insert(itemView: ItemView) {
        
        try! self.itemTable.insertRecord(itemView)
        itemView.itemRecordPrimaryKey = itemView.primaryKey
        
        try! self.itemDetailTable.insertRecord(itemView)
        itemView.itemDetailRecordPrimaryKey = itemView.primaryKey
        
        itemView.setNeedsLayout()
    }
    
    public func updateItemToDataCenter(itemView: ItemView) {
        
        var error: NSError?
        itemView.primaryKey = itemView.itemRecordPrimaryKey
        self.itemTable.updateRecord(itemView, error: &error)
        
        itemView.primaryKey = itemView.itemDetailRecordPrimaryKey
        self.itemDetailTable.updateRecord(itemView, error: &error)
    }
    
    public func updateItemFromDataCenter(itemView: ItemView) {
        
        self.changeRecordContentWithItem(itemRecordPrimaryKey: itemView.itemRecordPrimaryKey ?? 0, itemDetailRecordPrimaryKey: itemView.itemDetailRecordPrimaryKey ?? 0)
        
        let itemRecord = try! self.itemTable.find(withPrimaryKey: itemView.itemRecordPrimaryKey)
        _ = itemView.mergeRecord(itemRecord, shouldOverride: true)
        
        let itemDetailRecord = try! self.itemDetailTable.find(withPrimaryKey: itemView.itemDetailRecordPrimaryKey)
        _ = itemView.mergeRecord(itemDetailRecord, shouldOverride: true)
        
        itemView.setNeedsLayout()
    }
    
    public func deleteItem(itemView: ItemView) {
        
        var error: NSError?
        self.itemTable.delete(withPrimaryKey: itemView.itemRecordPrimaryKey, error: &error)
        self.itemDetailTable.delete(withPrimaryKey: itemView.itemDetailRecordPrimaryKey, error: &error)
        
        itemView.name = ""
        itemView.detail = "I have been deleted"
        
        itemView.setNeedsLayout()
    }
    
    //MARK: private methods
    private func changeRecordContentWithItem(itemRecordPrimaryKey: NSNumber, itemDetailRecordPrimaryKey: NSNumber) {
        
        var error: NSError?
        
        let itemRecord = try! self.itemTable.find(withPrimaryKey: itemRecordPrimaryKey)
        self.itemTable.updateRecord(itemRecord, error: &error)
        
        let itemDetailRecord = try! self.itemDetailTable.find(withPrimaryKey: itemDetailRecordPrimaryKey) as!ItemDetailRecord
        itemDetailRecord.detail = "this is a very big house"
        self.itemDetailTable.updateRecord(itemDetailRecord, error: &error)
    }
}
